package com.itbaizhan.rocketmqproducer.producer;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

//同步消息生产者（等待broker有消息返回才进行下步工作，可靠性高，效率慢）
public class SymcProducer {

    public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException, RemotingException, MQClientException, MQBrokerException {
        //实例化生产者，并指明生产组
        DefaultMQProducer producer = new DefaultMQProducer("producer_grp_01");
        //设置nameserver 地址
        producer.setNamesrvAddr("192.168.66.100:9876");
        //启动生产者
        producer.start();
        //创建消息,发送消息
        for(int i=0;i < 100;i++){
            //创建消息并指定topic ,Tag(标签)和消息体
            Message message = new Message("topic_springboot_01","TagA",
                    ("hello rocketmq...").getBytes(RemotingHelper.DEFAULT_CHARSET));
            //发送消息
            SendResult sendResult = producer.send(message);

            //打印返回的消息
            System.out.printf("%s\n",sendResult);
        }
        producer.shutdown();
    }

}
